import { reqLogin, reqUserMessage } from "@/api";
import { Message } from "element-ui";
import AES from "@/util/crypto";
import route from "@/route";
import Cookies from "js-cookie";
export default({
    namespaced:true,
    actions:{
        async toLoginByup(context,value){
            let result=await reqLogin(value);
            if(result.code==='200'){
                //登录请求成功后发送第二次请求，获取用户详细数据
                // context.commit('toLoginByup',result.message)
                if(value.rememberMe){
                    Cookies.set("usernameToken",AES.encrypt(value.username),{expires: 3 });
                    Cookies.set("passwordToken",AES.encrypt(value.password),{expires: 3 });
                }
                context.dispatch("getUserMessageByup",value.username);
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+'] \n 请检查账号、密码或联系超级管理员查询账号状态!',
                    type: 'warning'
                })
                return;
            }
        },
        async getUserMessageByup(context,value){
            let result=await reqUserMessage(value);
            if(result.code==='200'){
                context.commit('getUserMessageByup',result.other)
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        }
    },
    mutations:{
        updateUserMessage(state,value){
            console.log("enter")
            console.log(value)
            if(state.userObj.account===value.account){
                console.log(value)
                state.userObj.email=value.email;
                state.userObj.identity=value.identity;
                state.userObj.telephone=value.telephone;

                sessionStorage.removeItem("userObj");
                sessionStorage.setItem('userObj',AES.encrypt(JSON.stringify(state.userObj)))
            }
        },
        getUserMessageByup(state,value){
            state.userObj=Object.assign(state.userObj,value)
            sessionStorage.setItem('userObj',AES.encrypt(JSON.stringify(state.userObj)))
            route.replace({
                name:'Home'
            })  
        }
    },
    state:{
        userObj:{
            account:'未登录',
            telephone:'',
            identity:'',
            createtime:'',
            email:'',
            state:'',
            role:'None'
        }
    },
    getters:{
    
    }
})